//
//  EXiPhoneAdditions.m
//  MagicBase
//
//  Created by PH on 7.1.10.
//  Copyright 2010 Codesign. All rights reserved.
//

#import "EXiPhoneAdditions.h"

#ifdef PLBLOCKS
@implementation NSComparatorWrapper

@synthesize object;

- (id)initWithComparator:(NSComparator)_comp {
	if ((self = [super init])) {
		comp = _comp;
	}
	return self;
}

- (NSComparisonResult)compare:(NSComparatorWrapper*)wrapper {
	return comp(object, wrapper.object);
}

@end

@implementation NSArray (EXiPhoneAdditions)

- (NSArray*)sortedArrayUsingComparator:(NSComparator)comparator {
	NSMutableArray* array = [[self mutableCopy] autorelease];
	[array sortUsingComparator: comparator];
	return array;
}

@end

@implementation NSMutableArray (EXiPhoneAdditions)

- (void)sortUsingComparator:(NSComparator)comparator {
	NSMutableArray* wrappers = [[NSMutableArray alloc] initWithCapacity: [self count]];
	for (id object in self) {
		NSComparatorWrapper* wrapper = [[NSComparatorWrapper alloc] initWithComparator: comparator];
		wrapper.object = object;
		[wrappers addObject: wrapper];
		[wrapper release];
	}
	[wrappers sortUsingSelector: @selector(compare:)];
	[self removeAllObjects];
	for (NSComparatorWrapper* wrapper in wrappers) {
		[self addObject: wrapper.object];
	}
	[wrappers release];
}

@end
#endif
